package com.sugon.controller.spare;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.sugon.annotation.LimitSubmit;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.consts.SparePartScarpStatus;
import com.sugon.entity.ScarpListEntity;
import com.sugon.modules.sparepart.model.vo.SpScarpEaParam;
import com.sugon.service.ScarpListService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import com.sugon.utils.RRException;
import com.sugon.utils.excel.ExcelImport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 报废清单(新)Controller
 *
 * @author YuXD
 * @date 2020-02-10 16:37:15
 */
@Controller
@RequestMapping("scarplist")
public class ScarpListController {
    @Autowired
    private ScarpListService scarpListService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("scarplist:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ScarpListEntity> scarpListList = scarpListService.queryList(query);
        int total = scarpListService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(scarpListList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("scarplist:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ScarpListEntity scarpList = scarpListService.queryObject(id);

        return R.ok().put("scarpList", scarpList);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("scarplist:save")
    @ResponseBody
    public R save(@RequestBody ScarpListEntity scarpList) {
        scarpListService.save(scarpList);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("scarplist:update")
    @ResponseBody
    public R update(@RequestBody ScarpListEntity scarpList) {
        scarpListService.update(scarpList);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("scarplist:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        scarpListService.deleteBatch(ids);

        return R.ok();
    }

    @RequestMapping("/check")
    @RequiresPermissions("scarplist:check")
    @ResponseBody
    public R check(@RequestBody ScarpListEntity scarpList) {

        int flag = scarpListService.check(scarpList);

        return R.ok();
    }

    /**
     * 审核处理
     */
    @LimitSubmit(key = "checkSpScarp:%s")
    @NonBusinessParamValid
    @RequestMapping("/checkSpScarp")
    @ResponseBody
    public R checkSpScarp(@RequestBody @Validated SpScarpEaParam approveParam, BindingResult bindingResult) {
        List<String> idList = approveParam.getIdList();
        List<ScarpListEntity> scarpRecordList = CollectionUtil.newArrayList();
        for (String id : idList) {
            // 校验报废记录是否存在
            ScarpListEntity scarpRecord = scarpListService.queryObject(id);
            if (scarpRecord == null) {
                return R.error("报废记录不存在，请确认！");
            }
            // 只可以审核新增或审核中的报废记录
            if (NumberUtil.compare(scarpRecord.getStatus(), SparePartScarpStatus.NEW.getValue()) != 0 && NumberUtil.compare(scarpRecord.getStatus(), SparePartScarpStatus.CJ_EA_ING.getValue()) != 0 && NumberUtil.compare(scarpRecord.getStatus(), SparePartScarpStatus.PE_EA_ING.getValue()) != 0) {
                return R.error("只可以审核处于新建和审核中状态的报废记录！");
            }
            scarpRecordList.add(scarpRecord);
        }
        approveParam.setScarpRecordList(scarpRecordList);
        // 审核报废
        scarpListService.check(approveParam);
        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ScarpListEntity> list = scarpListService.queryList(params);

        return R.ok().put("list", list);
    }

    @RequestMapping("/import")
    @RequiresPermissions("scarplist:imports")
    @ResponseBody
    public R importE(@RequestParam("file") MultipartFile file) throws Exception {
        String err = "";
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        List list = ExcelImport.getExcelData(file);
        if (list != null && list.size() != 0) {
            String[] header = new String[]{"名称", "规格型号", "所属设备", "生产线", "报废数量", "现象描述", "判定与建议", "备注"};
            String[] data = (String[]) list.get(0);
            String[] data1 = (String[]) list.get(1);
            String[] data2 = (String[]) list.get(4);
            int j = 0;

            //String s1=data1;
            for (String d : data2) {
                if (header[j].equals(d.trim())) {
                    j++;
                }
            }
            if (j < header.length) {
                err = "找不到\"" + header[j] + "\"这一列";
            }
            if (err.equals("")) {
                scarpListService.delete(null);
                for (int i = 5; i < list.size(); i++) {
                    ScarpListEntity scarpListEntity = new ScarpListEntity();
                    scarpListEntity.setValues(data1, (String[]) list.get(i));
                    if (list.get(i) != null && scarpListEntity.getName() != "") {
                        scarpListService.save(scarpListEntity);
                    }

                }
            }
        }

        return R.ok().put("err", err);
    }

}
